package com.user.service.impl;

import com.user.model.entity.PayInfo;
import com.user.model.entity.ServerChuZu;
import com.user.model.entity.ServerZuLin;
import com.user.model.entity.User;
import com.user.model.vo.ServerZulinVo;
import com.user.model.vo.ZulinListVo;
import com.user.param.Result;
import com.user.repositories.*;
import com.user.service.ServerZuLinService;
import com.user.util.CatchUtil;
import com.user.util.ChangeUtil;
import com.user.util.GetUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ServerZuLinServiceImpl implements ServerZuLinService {

    @Autowired
    ServerZuLinRepository serverZuLinRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    ServerInfoRepository serverInfoRepository;

    @Autowired
    ServerChuZuRepository serverChuZuRepository;

    @Resource
    PayInfoRepository payInfoRepository;

    @Override
    @Transactional
    public Result serverZuLin(ServerZulinVo serverZulinVo) {

        User user = new User();
        user = GetUserUtil.loginUser();

        ServerZuLin serverZuLin = new ServerZuLin();

        ChangeUtil.transalte(serverZulinVo, serverZuLin);

        serverZuLin.setUserId(user.getId());
        serverZuLin.setServerCzId(serverZulinVo.getServerCzId());

        ServerChuZu serverChuZu = serverChuZuRepository.findByIdAndDeleted(serverZuLin.getServerCzId(),'1');

        //保存租赁信息
        serverZuLin.setUserId(user.getId());
        serverZuLin.setServerId(serverChuZu.getServerId());
        serverZuLin.setStarTime(serverChuZu.getStarTime());
        serverZuLin.setEndTime(serverChuZu.getEndTime());
        serverZuLin = serverZuLinRepository.save(serverZuLin);

        //更新出租信息
        serverChuZu.setState('Y');
        serverChuZu.setUpdateTime(new Date());
        serverChuZuRepository.save(serverChuZu);

        //保存支付信息
        PayInfo payInfo = new PayInfo();
        payInfo.setServerCzId(serverChuZu.getId());
        payInfo.setServerId(serverChuZu.getServerId());
        payInfo.setZuUserId(user.getId());
        payInfo.setMoney(serverChuZu.getMoney());
        payInfo.setPayTime(new Date());
        payInfo.setDeleted('1');
        payInfo.setCreateTime(new Date());
        payInfo.setUpdateTime(new Date());
        payInfoRepository.save(payInfo);

        //返回执行结果
        if (null != serverZuLin.getId()) {
            return new Result(Result.SUCCESS_CODE);
        } else {
            return new Result(Result.ERROR_CODE);
        }
    }

    @Override
    public Result zulinList(ServerZulinVo serverZulinVo) {
        List<ServerZuLin> serverZuLins = serverZuLinRepository.findAll();
        List<ZulinListVo> list = new ArrayList<ZulinListVo>();
        for (ServerZuLin serverZuLin : serverZuLins) {
            ZulinListVo zulinListVo = new ZulinListVo();
            zulinListVo.setServerZuLin(serverZuLin);
            zulinListVo.setUser(userRepository.findByIdAndDeleted(serverZuLin.getUserId(), 'N'));
            zulinListVo.setServerInfo(serverInfoRepository.findByIdAndDeleted(serverZuLin.getServerId(), 'N'));
            zulinListVo.setServerChuZu(serverChuZuRepository.findByIdAndDeleted(serverZuLin.getServerCzId(), 'N'));
            list.add(zulinListVo);
        }
        return new Result(list);
    }

    @Override
    public Result geZulinList(HttpServletRequest request) {
        User user = new User();
        user = GetUserUtil.loginUser();
        String userId = user.getId();


        List<ServerZuLin> serverZuLins = serverZuLinRepository.findAllByUserIdAndDeleted(userId,'1');
        List<ZulinListVo> list = new ArrayList<ZulinListVo>();
        for (ServerZuLin serverZuLin : serverZuLins) {
            ZulinListVo zulinListVo = new ZulinListVo();
            zulinListVo.setServerZuLin(serverZuLin);
            zulinListVo.setUser(userRepository.findByIdAndDeleted(serverZuLin.getUserId(), '1'));
            zulinListVo.setServerInfo(serverInfoRepository.findByIdAndDeleted(serverZuLin.getServerId(), '1'));
            zulinListVo.setServerChuZu(serverChuZuRepository.findByIdAndDeleted(serverZuLin.getServerCzId(), '1'));
            list.add(zulinListVo);
        }
        return new Result(list);
    }
}
